import { _decorator, Component, director, Node, profiler } from "cc";
import { loadPool } from "./res/loadPool";
import { bgmName, emits, pingtai } from "./data/enmus";
import { audioTool } from "./untils/audioTool";
import { gameConfig } from "./data/gameConfig";
import { wxAd } from "./AD/wxAd";
import { loadRes } from "./res/loadRes";
import { adMgr } from "./AD/adMgr";
import { dyAd } from "./AD/dyAd";
const { ccclass, property } = _decorator;

@ccclass("homeView")
export class homeView extends Component {
  // 侧边栏按钮
  @property(Node)
  cebian: Node = null;
  start() {
    profiler.hideStats();
    this.init();
    // 打开首页banner
    adMgr.showBanner();
    adMgr.hideGezi();
    adMgr.showChaPing();
    // 监听音频中断
    if (gameConfig.system == pingtai.wx) {
      wxAd.ins.bgmZd(() => {
        audioTool.ins.playMusic(bgmName.music_bg);
      });
    }
  }

  init() {
    audioTool.ins.playMusic(bgmName.music_bg);
    // 判断是否显示侧边栏按钮
    if (gameConfig.system == pingtai.dy) {
      dyAd.ins.checkSidebar(
        () => {
          this.cebian.active = true;
        },
        () => {
          this.cebian.active = false;
        }
      );
    }
    // 预加载下一关
    director.preloadScene(
      "main",
      (completedCount: number, totalCount: number, item: any) => {
        // console.log(completedCount, totalCount);
      },
      () => {
        console.log("主界面预加载完成");
      }
    );
  }
  // 打开设置
  settingBtn() {
    audioTool.ins.playSound(bgmName.sound_btn);
    loadPool.ins.getPoolNode("setting", this.node);
    director.emit(emits.backIsShow);
  }
  // 打开排行
  paihangBtn() {
    audioTool.ins.playSound(bgmName.sound_btn);
    loadPool.ins.getPoolNode("tips", this.node);
    director.emit(emits.tipMsg, "功能暂未开放");
  }
  // 打开侧边栏
  cebianBtn() {
    audioTool.ins.playSound(bgmName.sound_btn);
    loadPool.ins.getPoolNode("cebianPop", this.node);
  }
  // 分享游戏
  shareBtn() {
    audioTool.ins.stopMusic();
    adMgr.zhuanFa();
    this.scheduleOnce(() => {
      audioTool.ins.playMusic(bgmName.music_bg);
    }, 0.3);
  }
  // 皮肤商店
  pifuBtn() {
    audioTool.ins.playSound(bgmName.sound_btn);
    loadPool.ins.getPoolNode("pifuShop", this.node);
  }
  // 开始游戏
  startBtn() {
    audioTool.ins.playSound(bgmName.sound_btn);
    audioTool.ins.stopMusic();
    director.loadScene("main");
  }
  // 销毁组件
  protected onDestroy(): void {
    this.unscheduleAllCallbacks();
  }
}
